// radio.js
const EventEmitter = require('events');
const util = require('util');

function Radio(stationName, frequency) {
    EventEmitter.call(this);
    this.stationName = stationName;
    this.frequency = frequency;
}

// 使用 util.inherits 实现继承
util.inherits(Radio, EventEmitter);

Radio.prototype.play = function() {
    const self = this;
    this.emit('open', `${this.stationName} ${this.frequency} opened`);
    
    // 模拟播放声音
    setTimeout(() => {
        console.log('lalala...');
        self.stop(); // 播放完成后自动关闭
    }, 2000);
};

Radio.prototype.stop = function() {
    this.emit('close', `${this.stationName} ${this.frequency} closed`);
};

module.exports = Radio;